package com.scetop.bank;

public class CheckingAccount extends Account {
    private final SavingsAccount protectedBy;

    public CheckingAccount(double balance) {
        this(balance, null);
    }

    public CheckingAccount(double balance, SavingsAccount protectedBy) {
        super(balance);
        this.protectedBy = protectedBy;
    }

    @Override
    public boolean withdraw(double amt) {
        if (balance >= amt) {
            balance -= amt;
            return true;
        } else if (protectedBy != null) {
            double overdraftNeeded = amt - balance;
            if (protectedBy.getBalance() >= overdraftNeeded) {
                balance = 0;
                protectedBy.withdraw(overdraftNeeded);
                return true;
            }
        }
        return false;
    }
}